package com.ruoyi.web.controller.agriculture;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.agriculture.domain.po.AgActivity;
import com.ruoyi.system.agriculture.service.IAgActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 农业活动Controller
 */
@RestController
@RequestMapping("/agriculture/activity")
public class AgActivityController extends BaseController {
    @Autowired
    private IAgActivityService activityService;

    /**
     * 查询农业活动列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgActivity activity) {
        startPage();
        List<AgActivity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 导出农业活动列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:export')")
    @Log(title = "农业活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgActivity activity) {
        LambdaQueryWrapper<AgActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(activity.getActivityName() != null, AgActivity::getActivityName, activity.getActivityName())
                .eq(activity.getActivityType() != null, AgActivity::getActivityType, activity.getActivityType())
                .eq(activity.getStatus() != null, AgActivity::getStatus, activity.getStatus())
                .like(activity.getResponsiblePerson() != null, AgActivity::getResponsiblePerson, activity.getResponsiblePerson());
        List<AgActivity> list = activityService.list(queryWrapper);
        ExcelUtil<AgActivity> util = new ExcelUtil<>(AgActivity.class);
        return util.exportExcel(list, "农业活动数据");
    }

    /**
     * 获取农业活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId) {
        return success(activityService.getById(activityId));
    }

    /**
     * 新增农业活动
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:add')")
    @Log(title = "农业活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgActivity activity) {
        return toAjax(activityService.save(activity));
    }

    /**
     * 修改农业活动
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:edit')")
    @Log(title = "农业活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgActivity activity) {
        return toAjax(activityService.updateById(activity));
    }

    /**
     * 删除农业活动
     */
    @PreAuthorize("@ss.hasPermi('agriculture:activity:remove')")
    @Log(title = "农业活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds) {
        return toAjax(activityService.removeBatchByIds(List.of(activityIds)));
    }
} 